<?php
namespace app\admin\controller;
use think\Db;
use think\Validate;
use app\admin\controller\Base;
use app\admin\model\Websites as WebsitesModel;

class Websites extends Base{

	public function lists(){
		$list=WebsitesModel::paginate(10);
		$this->assign('list',$list);
		return $this->fetch('listuser');
	}
	/*******
	// 会员详情
	*****/
	public function detail(){
		$id=input('Id');
		$res=db('Websites')->find($id);
		$this->assign('detailinfo',$res);
		return $this->fetch('listuserdetail');
	}


	/*****
	edit()超值机票推荐的信息编辑
	*****/
	public function edit(){
		$Mongo=Mongoexa();
		
		switch (input()['type']) {
			case '1':
				if (request()->isPost()) {
					$result=$Mongo->insert(array("title"=>"超值机票",
															"singleinfo"=>array(array("price"=>input('price'),
												"airname"=>input('airname'),
												"startplace"=>input('startplace'),
												"endplace"=>input('endplace'),
												"flytime"=>input('flytime'),
												"specials"=>input('specials'),
												"click"=>input('click'),
												)
															)));
					if ($result['ok']) {
						return $this->success('新增成功！','article/goodsintro');
					}else{
						return $this->success('新增失败！');
					}
				}
				break;
			case '2':
				if (request()->isPost()) {
					$result=$Mongo->update(array('_id'=>mongoid(input('_id'))),array("title"=>"超值机票",
															"singleinfo"=>array(array("price"=>input('price'),
												"airname"=>input('airname'),
												"startplace"=>input('startplace'),
												"endplace"=>input('endplace'),
												"flytime"=>input('flytime'),
												"specials"=>input('specials'),
												"click"=>input('click'),
												)
															)));
					if ($result['updatedExisting']) {
						return $this->success('修改成功！','article/goodsintro');
					}else{
						return $this->success('修改失败！');
					}
				}
				$id=input();
				$result=$Mongo->find(array('_id'=>mongoid($id['Id'])));
				$result=array_values(iterator_to_array($result));
				$this->assign('result',$result);		
				break;	
		}
		$this->assign('type',input()['type']);
		return $this->fetch('article/introdetail');
	}

	/****
	editgood()产品推荐的信息编辑
	****/
	public function editgood(){
		$Mongo=Mongoexa();
		$id=input();
		switch ($id['type']) {
			// 1新增
			// 2编辑
			case '1':
				if(request()->isPost()){
					if($_FILES['uploadImage']['tmp_name']!=''&&isset($_FILES['uploadImage']['tmp_name'])){
						$file=request()->file('uploadImage');
						$info=$file->move(ROOT_PATH.'public'.DS.'static/uploads');
						$id['picurl']='/static/uploads/'.$info->getSaveName();
					}
					$res=$Mongo->insert(array("title"=>"超值套餐",
															"singleinfo"=>array(array("price"=>$id['price'],
												"name"=>$id['name'],
												"picurl"=>$id['picurl'],
												"specials"=>$id['specials'],
												"click"=>$id['click'],
												)
															)));
					if ($res['ok']) {
						
						return $this->success('添加成功！','article/goodsintro');
					}else{
						return $this->success('添加失败！');
					}
				}	
				break;

			case '2':
				$result=$Mongo->find(array('_id'=>mongoid($id['_id'])));
				$result=array_values(iterator_to_array($result));
				if(request()->isPost()){
					if($_FILES['uploadImage']['tmp_name']!=''&&isset($_FILES['uploadImage']['tmp_name'])){
						$file=request()->file('uploadImage');
						$info=$file->move(ROOT_PATH.'public'.DS.'static/uploads');
						$id['picurl']='/static/uploads/'.$info->getSaveName();
					}
					$res=$Mongo->update(array('_id'=>mongoid(input('_id'))),array("title"=>"超值套餐",
															"singleinfo"=>array(array("price"=>$id['price'],
												"name"=>$id['name'],
												"picurl"=>$id['picurl'],
												"specials"=>$id['specials'],
												"click"=>$id['click'],
												)
															)));
					if ($res['updatedExisting']) {
						if ($result[0]['singleinfo'][0]['picurl']!='' && file_exists(ROOT_PATH.'public'.$result[0]['singleinfo'][0]['picurl'])) {
							unlink(ROOT_PATH.'public'.$result[0]['singleinfo'][0]['picurl']);
						}
						return $this->success('修改成功！','article/goodsintro');
					}else{
						return $this->success('修改失败！');
					}
				}	
				$this->assign('result',$result);
				break;
		}
		$this->assign('type',$id['type']);
		return $this->fetch('article/introgooddetail');
	}


	/*****
	delete()删除推荐的产品信息(包括飞机机票和特价套餐)
	****/
	public function delete(){
		$Mongo=Mongoexa();
		$id=input();
		$result=$Mongo->remove(array('_id'=>mongoid($id['_id'])), array("justOne" => true));
		$result=mongofunc();
		$resultc=array();
		foreach ($result as $key => $value) {
			if(!isset($resultc[$value['title']])){
				$resultc[$value['title']]=array();
			}
			$resultc[$value['title']][]=array('id'=>$value['_id'],'title'=>$value['title'],'singleinfo'=>$value['singleinfo'][0]);
		}
		$result=$resultc;
		$this->assign('result',$result);
		return $this->fetch('article/list');
	}



}